package com.itlong.cloud.commons.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.property.PropertyPaymentTradeDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyPaymentTradeGetPageDTO;
import com.itlong.cloud.POJO.PO.HuaweiOrderInfoPO;
import com.itlong.cloud.POJO.VO.property.PropertyPaymentTradeInfoAllVO;
import com.itlong.cloud.POJO.VO.property.PropertyPaymentTradeInfoVO;
import com.itlong.cloud.POJO.VO.property.PropertyPaymentTradeVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.commons.dao.IPaymentTradeDao;
import com.itlong.cloud.commons.service.IPaymentTradeService;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.enumerate.AssessSyncDataTypeEnum;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      订单管理服务层接口实现类
 * </desc>
 *
 * @createDate 2019/05/23
 */
@Service
@RefreshScope
public class PaymentTradeServiceImpl implements IPaymentTradeService {

    @Value("${"+ PlatformConstants.MCHID +"}")
    private String MCHID;

    @Autowired
    private IPaymentTradeDao iPaymentTradeDao;

    @Autowired
    MessageSender messageSender;

    /**
     * <desc>
     *      分页查询付费通行订单
     * </desc>
     *
     * @param propertyPaymentTradeDTO 订单管理DTO
     * @return 付费通行订单分页信息
     * @author wangzhi
     * @createDate 2019/05/23
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyPaymentTradeVO> getPage(PropertyPaymentTradeDTO propertyPaymentTradeDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyPaymentTradeDTO);
        Page<PropertyPaymentTradeVO> page = new Page<>();
        page.getPage().setPageSize(propertyPaymentTradeDTO.getPageSize());
        page.getPage().setCurrentPage(propertyPaymentTradeDTO.getCurrentPage());
        params.put("startSlip", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        List<PropertyPaymentTradeVO> propertyPaymentTradeVOS = iPaymentTradeDao.getPage(params);
        page.setRows(propertyPaymentTradeVOS,iPaymentTradeDao.getRecordCount(params));
        return page;
    }

    /**
     * <desc>
     *      查询导出的付费通行订单列表
     * </desc>
     *
     * @param propertyPaymentTradeDTO 订单管理DTO
     * @return 导出的付费通行订单列表
     * @author wangzhi
     * @createDate 2019/05/23
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<PropertyPaymentTradeVO> getExportData(PropertyPaymentTradeDTO propertyPaymentTradeDTO) throws Exception {
        if (StringUtils.isBlank(propertyPaymentTradeDTO.getTradeIds())) {
            Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyPaymentTradeDTO);
            return iPaymentTradeDao.getAllData(params);
        }
        List<String> tradeIdList = StringHandlerUtil.splitStringList(propertyPaymentTradeDTO.getTradeIds());
        return iPaymentTradeDao.getDataByTradeIds(tradeIdList, propertyPaymentTradeDTO.getProjectId());
    }

    /**
     * <desc>
     *      分页查询所有支付订单
     * </desc>
     *
     * @param propertyPaymentTradeGetPageDTO 支付订单查询参数DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2019/10/18
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyPaymentTradeInfoVO> getPaymentTradeByPage(PropertyPaymentTradeGetPageDTO propertyPaymentTradeGetPageDTO) throws Exception {
        //将参数转换为Map类型
        propertyPaymentTradeGetPageDTO.setMchid(MCHID);
        Map<String,Object> params=SqlUtil.convertBeanToMapParams(propertyPaymentTradeGetPageDTO);
        Page<PropertyPaymentTradeInfoVO> page=new Page<>();
        page.getPage().setCurrentPage(propertyPaymentTradeGetPageDTO.getCurrentPage());
        page.getPage().setPageSize(propertyPaymentTradeGetPageDTO.getPageSize());
        params.put("startSlip",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        List<PropertyPaymentTradeInfoVO> propertyPaymentTradeInfoVOS=iPaymentTradeDao.getPaymentTradeByPage(params);
        page.setRows(propertyPaymentTradeInfoVOS,iPaymentTradeDao.getPaymentTradeCount(params));
        return page;
    }

    /**
     * <desc>
     *      运营分页查询直接支付订单列表
     * </desc>
     *
     * @param propertyPaymentTradeGetPageDTO
     * @return
     * @Author Zhu.yj
     * @createDate  2019/10/23
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyPaymentTradeInfoAllVO getOperatePaymentTradeByPage(PropertyPaymentTradeGetPageDTO propertyPaymentTradeGetPageDTO) throws Exception {
        PropertyPaymentTradeInfoAllVO propertyPaymentTradeInfoAllVO = new PropertyPaymentTradeInfoAllVO();
        Map<String ,Object> params = SqlUtil.convertBeanToMapParams(propertyPaymentTradeGetPageDTO);
        Page<PropertyPaymentTradeInfoVO> page=new Page<>();
        page.getPage().setCurrentPage(propertyPaymentTradeGetPageDTO.getCurrentPage());
        page.getPage().setPageSize(propertyPaymentTradeGetPageDTO.getPageSize());
        params.put("startSlip",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        page.setRows(iPaymentTradeDao.getOperatePaymentTradeByPage(params),iPaymentTradeDao.getOperatePaymentTradeCount(params));
        String priceCount=iPaymentTradeDao.getOperatePaymentTradePriceCount(params);
        propertyPaymentTradeInfoAllVO.setPriceCount(priceCount);
        propertyPaymentTradeInfoAllVO.setPage(page);
        return propertyPaymentTradeInfoAllVO;
    }

    /**
     * <desc>
     *      统计运营直接到账的支付订单总金额
     * </desc>
     *
     * @param
     * @return
     * @Author Zhu.yj
     * @createDate  2019/10/30
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public String getOperatePaymentTradePriceCount() throws Exception{
        Map<String,Object> params=new HashMap<>();
        params.put("tradeStatus","2");
        return iPaymentTradeDao.getOperatePaymentTradePriceCount(params);
    }

    /**
     * <desc>
     *      保存华为SaaS信息
     * </desc>
     *
     * @param po
     * @return
     * @Author Qiang.S
     * @createDate  2020/04/22
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void saveHuaweiSaaSData(HuaweiOrderInfoPO po) throws Exception{
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",po);
        jsonObject.put("type", AssessSyncDataTypeEnum.ORDER.getType());
        messageSender.send(RabbitMQConstant.RABBITMQ_ASSESS_SYNC_DATA_QUEUE, jsonObject.toJSONString(),
                RabbitMQConstant.RABBITMQ_SYNC_DATA_EXCHANGE, RabbitMQConstant.RABBITMQ_ASSESS_SYNC_DATA_KEY);
        iPaymentTradeDao.save(SqlUtil.durableData(po, PlatformConstants.TABLE_SAVE));
    }


}
